%macro raices 4	; src dst_l dst_h aux

	%define src %1
	%define dst_l %2
	%define aux_l %3
	%define dst_h %3
	%define aux_h %4
	

	; asume xmm7 = 0
	; asume xmm6 = sqrt(2) | sqrt(2) | sqrt(2) | sqrt(2)

	;--------------+
	;     low      ;
	;--------------+

		; Desempaqueto src - low

		movdqu dst_l,%1

		punpcklbw dst_l,xmm7		; xmm3 = low

		movdqu aux_l, dst_l
		punpcklwd dst_l,xmm7		; xmm3 = low-low
		punpckhwd aux_l,xmm7		; xmm4 = low-high

		; convierto a floating point

		cvtdq2ps dst_l, dst_l
		cvtdq2ps aux_l, aux_l

		; multiplico c/u por sqrt(2)

		mulps dst_l, xmm6
		mulps aux_l, xmm6
		
		;multiploco por 16
		mov dword [esp-4],16
		movd xmm7,[esp-4]
		pshufd xmm7,xmm7,0
		cvtdq2ps xmm7,xmm7
		mulps dst_l,xmm7
		mulps aux_l,xmm7
		pxor xmm7,xmm7

		; paso a integer de nuevo

		cvtps2dq dst_l, dst_l
		cvtps2dq aux_l, aux_l

		; empaco a words (tal vez pierdo un poco de precision aca)

		packssdw dst_l, aux_l

	;--------------+
	;     high     ;
	;--------------+

		; Desempaqueto xmm1 - low

		movdqu dst_h,%1

		punpckhbw dst_h,xmm7		; xmm3 = low

		movdqu aux_h, dst_h
		punpcklwd dst_h,xmm7		; xmm3 = low-low
		punpckhwd aux_h,xmm7		; xmm4 = low-high

		; convierto a floating point

		cvtdq2ps dst_h, dst_h
		cvtdq2ps aux_h, aux_h

		; multiplico c/u por sqrt(2)

		mulps dst_h, xmm6
		mulps aux_h, xmm6
		
		;multiploco por 16
		mov dword [esp-4],16
		movd xmm7,[esp-4]
		pshufd xmm7,xmm7,0
		cvtdq2ps xmm7,xmm7
		mulps dst_h,xmm7
		mulps aux_h,xmm7
		pxor xmm7,xmm7

		; paso a integer de nuevo

		cvtps2dq dst_h, dst_h
		cvtps2dq aux_h, aux_h

		; empaco a words (tal vez pierdo un poco de precision aca)

		packssdw dst_h, aux_h
%endmacro

%macro raices2 4	; src dst_l dst_h aux

	%define src %1
	%define dst_l %2
	%define aux_l %3
	%define dst_h %3
	%define aux_h %4

	;--------------+
	;     low      ;
	;--------------+

		; Desempaqueto src - low

		movdqu dst_l,%1

		pxor xmm7,xmm7
		pcmpgtb xmm7, dst_l
		punpcklbw dst_l,xmm7		; dst_l = low(src)

		pxor xmm7,xmm7
		pcmpgtw xmm7, dst_l
		movdqu aux_l, dst_l
		punpcklwd dst_l,xmm7		; xmm3 = low-low
		punpckhwd aux_l,xmm7		; xmm4 = low-high

		; convierto a floating point

		cvtdq2ps dst_l, dst_l
		cvtdq2ps aux_l, aux_l

		; multiplico c/u por sqrt(2)

		movss xmm7, AUX
		pshufd xmm7, xmm7, 00000000b

		mulps dst_l, xmm7
		mulps aux_l, xmm7

		; paso a integer de nuevo

		cvtps2dq dst_l, dst_l
		cvtps2dq aux_l, aux_l

		; empaco a words (tal vez pierdo un poco de precision aca)

		packssdw dst_l, aux_l

	;--------------+
	;     high     ;
	;--------------+

		; Desempaqueto xmm1 - low

		movdqu dst_h,%1

		pxor xmm7,xmm7
		pcmpgtb xmm7, dst_h
		punpckhbw dst_h,xmm7		; xmm3 = low

		pxor xmm7,xmm7
		pcmpgtw xmm7, dst_h
		movdqu aux_h, dst_h
		punpcklwd dst_h,xmm7		; xmm3 = low-low
		punpckhwd aux_h,xmm7		; xmm4 = low-high

		; convierto a floating point

		cvtdq2ps dst_h, dst_h
		cvtdq2ps aux_h, aux_h

		; multiplico c/u por sqrt(2)

		movss xmm7, AUX
		pshufd xmm7, xmm7, 00000000b

		mulps dst_h, xmm7
		mulps aux_h, xmm7

		; paso a integer de nuevo

		cvtps2dq dst_h, dst_h
		cvtps2dq aux_h, aux_h

		; empaco a words (tal vez pierdo un poco de precision aca)

		packssdw dst_h, aux_h
%endmacro
